﻿namespace LiteBlog.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web.Mvc;
    using System.Web.Routing;
    using System.Xml;

    using LiteBlog.Common;
    using LiteBlog.Components;
    using LiteBlog.Common.Entities;
    using LiteBlog.Common.Components;
    using LiteBlog.Common.Enums;
    using LiteBlog.Components.Contracts;
    using System.Web.Script.Serialization;
    using LiteBlog.Controllers.Helpers;
    using LiteBlog.Controllers.Filters;
    using System.Drawing;
    using LiteBlog.Models;
    using LiteBlog.Controllers.Contracts;

    public class HomeController : BaseController, IHomeController
    {
        private IPageComp _pageComp;
        private IBlogComp _blogComp;
        private IPostComp _postComp;
        private IStatComp _statComp;
        private IFeedComp _feedComp;
        private IAuthorComp _authorComp;

        public IPageComp PageComponent
        {
            get
            {
                return _pageComp;
            }
        }
        public IBlogComp BlogComponent
        {
            get
            {
                return _blogComp;
            }
        }

        public IPostComp PostComponent
        {
            get
            {
                return _postComp;
            }
        }

        public IStatComp StatComponent
        {
            get
            {
                return _statComp;
            }
        }
        public IFeedComp FeedComponent
        {
            get
            {
                return _feedComp;
            }
        }
        public IAuthorComp AuthorComponent
        {
            get
            {
                return _authorComp;
            }
        }

        public HomeController(IPageComp pageComp, 
            IBlogComp blogComp, 
            IPostComp postComp, 
            IFeedComp feedComp, 
            IAuthorComp authorComp, 
            IStatComp statComp, 
            ISettingsComp settingsComp, 
            ICacheComp cacheComp) : base(settingsComp, cacheComp)
        {
            _pageComp = pageComp;
            _blogComp = blogComp;
            _postComp = postComp;
            _feedComp = feedComp;
            _authorComp = authorComp;
            _statComp = statComp;
        }

        public ActionResult Atom()
        {
            StatComponent.IncrementFeeds();
            var feed = FeedComponent.GetFeed();
            return new FeedActionResult(feed, FeedType.ATOM);
        }

        public ActionResult Rss()
        {
            StatComponent.IncrementFeeds();
            var feed = FeedComponent.GetFeed();
            return new FeedActionResult(feed, FeedType.RSS);
        }

        public ActionResult Error()
        {
            return View("Error", GetSiteLayout(), null);
        }

        [HttpGet]
        [CrawlerFilterForPosts]
        public ActionResult Index(int? page)
        {
            var postInfos = BlogComponent.GetPublished();
            var postCollectionModel = new PostCollectionModel();
            int postCount = 0;
            if (postInfos != null)
                postCount = postInfos.Count;

            page = page ?? 1;
            var settings = GetSettings();
            var pageSize = settings.PostCount;
            postCollectionModel.Page = page.Value;
            postCollectionModel.PageCount = postCount / pageSize + ((postCount % pageSize == 0) ? 0 : 1);
            if (postCollectionModel.PageCount == 0)
                postCollectionModel.PageCount = 1;
            page = Math.Min(page.Value, postCollectionModel.PageCount);
            var posts = new List<Post>();
            if (postInfos != null && postCount > 0)
            {
                int skip = (page.Value - 1) * pageSize;
                foreach (var postInfo in postInfos.Skip(skip).Take(pageSize))
                {
                    var post = PostComponent.Load(postInfo.FileID);
                    DisplayHelper.SetDisplayTime(post);
                    posts.Add(post);
                }
            }
            postCollectionModel.PostCollection = posts;
            postCollectionModel.UrlType = UrlType.Home;
            if(RouteData!=null)
                RouteData.Values["posts"] = postCollectionModel;
            if (UserModel!=null && UserModel.Authenticated)
                postCollectionModel.Author = AuthorComponent.Get(UserModel.Name);
            return View("Index", GetSiteLayout(), postCollectionModel);
        }

        public ActionResult Widget()
        {
            StatComponent.IncrementHits();
            return PartialView();
        }

        public ActionResult Statistics()
        {
            var stat = StatComponent.Load();
            return View("Statistics", GetSiteLayout(), stat);
        }


        public ActionResult Title()
        {
            var model = new TitleModel();
            var settings = GetSettings();
            ViewBag.Title = model.Title = settings.Name;
            model.Tagline = settings.Tagline;
            ViewBag.ImageUrl = model.ImageUrl = settings.ImageUrl;
            ViewBag.UseImage = model.UseImage = settings.UseImage;
            var pages = PageComponent.GetPagesInHeaderMenu();
            model.Pages = pages;

            if (HttpContext != null)
            {
                model.IsSite = (bool)HttpContext.Application["IsSite"];
                model.Site = (string)HttpContext.Application["Site"];
            }

            model.Skins = new Dictionary<string, Color>();
            foreach (var skin in Enum.GetNames(typeof(Skin)))
            {
                model.Skins[skin] = SkinHelper.GetSkinColor(skin);
            }
            var currentSkin = SkinHelper.GetSkin();
            model.Skin = SkinHelper.GetSkinColor(currentSkin);

            return PartialView("TitleControl", model);
        }

        public ActionResult GA()
        {
            var settings = GetSettings();
            return View(settings);
        }

        public void ResolveComponents()
        {

        }
    }
}